#include "patienteditview.h"
#include "ui_patienteditview.h"
#include "idatabase.h"
#include <QSqlTableModel>
PatientEditView::PatientEditView(QWidget *parent,int index)
    : QWidget(parent)
    , ui(new Ui::PatientEditView)
{
    ui->setupUi(this);

    dataMapper = new QDataWidgetMapper();
    QSqlTableModel *tabModel = IDatabase::getInstance().patientTabModel;
    dataMapper->setModel(IDatabase::getInstance().patientTabModel);
    dataMapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);

    dataMapper->addMapping(ui->dbEditID,tabModel->fieldIndex("id"));
    dataMapper->addMapping(ui->dbEditName,tabModel->fieldIndex("name"));
    dataMapper->addMapping(ui->dbEditIDCard,tabModel->fieldIndex("id_card"));
    dataMapper->addMapping(ui->dbSpinHeight,tabModel->fieldIndex("height"));
    dataMapper->addMapping(ui->dbSpinWeight,tabModel->fieldIndex("weight"));
    dataMapper->addMapping(ui->dbEditMobile,tabModel->fieldIndex("mobilePhone"));
    dataMapper->addMapping(ui->dbDateEditDOB,tabModel->fieldIndex("dob"));
    dataMapper->addMapping(ui->dbCreatedTimeStamp,tabModel->fieldIndex("createdTimeStamp"));

    dataMapper->setCurrentIndex(index);
}

PatientEditView::~PatientEditView()
{
    delete ui;
}

void PatientEditView::on_btnSave_clicked()
{
    IDatabase::getInstance().submitPatient();

    emit goPreviousView();
}


void PatientEditView::on_btnCancel_clicked()
{
    IDatabase::getInstance().revertPatient();
    emit goPreviousView();
}

